package com.library.management.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.library.common.annotation.Anonymous;
import com.library.management.domain.vo.LibraryBorrowVo;
import com.library.management.service.ILibraryBorrowService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.library.common.annotation.Log;
import com.library.common.core.controller.BaseController;
import com.library.common.core.domain.AjaxResult;
import com.library.common.enums.BusinessType;
import com.library.management.domain.LibraryBorrow;
import com.library.common.utils.poi.ExcelUtil;
import com.library.common.core.page.TableDataInfo;

/**
 * 借阅记录Controller
 * 
 * @author library
 * @date 2023-03-26
 */
@RestController
@RequestMapping("/management/borrow")
public class LibraryBorrowController extends BaseController
{
    @Autowired
    private ILibraryBorrowService libraryBorrowService;
    /**
     * 还书
     */
    @Anonymous
    @Log(title = "还书", businessType = BusinessType.UPDATE)
    @GetMapping("/returnBooks/{ids}")
    public AjaxResult returnBooks(@PathVariable String[] ids) {
        return toAjax(libraryBorrowService.returnBooks(ids));
    }
    /**
     * 续借
     */
    @Anonymous
    @Log(title = "续借", businessType = BusinessType.UPDATE)
    @GetMapping("/renew/{ids}")
    public AjaxResult renew(@PathVariable String[] ids) {
        return toAjax(libraryBorrowService.renew(ids));
    }

    /**
     * 查询借阅记录列表
     */
//    @PreAuthorize("@ss.hasPermi('management:borrow:list')")
    @GetMapping("/list")
    public TableDataInfo list(LibraryBorrow libraryBorrow)
    {
        startPage();
        List<LibraryBorrowVo> list = libraryBorrowService.selectLibraryBorrowList(libraryBorrow);
        return getDataTable(list);
    }
    /**
     * 查询自己借阅记录列表
     */
//    @PreAuthorize("@ss.hasPermi('management:borrow:list')")
    @GetMapping("/mylist")
    public TableDataInfo myList(LibraryBorrow libraryBorrow)
    {
        startPage();
        List<LibraryBorrowVo> list = libraryBorrowService.selectLibraryBorrowList1(libraryBorrow);
        return getDataTable(list);
    }
    /**
     * 导出借阅记录列表
     */
    @PreAuthorize("@ss.hasPermi('management:borrow:export')")
    @Log(title = "借阅记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, LibraryBorrow libraryBorrow)
    {
        List<LibraryBorrowVo> list = libraryBorrowService.selectLibraryBorrowList(libraryBorrow);
        ExcelUtil<LibraryBorrowVo> util = new ExcelUtil<LibraryBorrowVo>(LibraryBorrowVo.class);
        util.exportExcel(response, list, "借阅记录数据");
    }

    /**
     * 获取借阅记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('management:borrow:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(libraryBorrowService.selectLibraryBorrowById(id));
    }

    /**
     * 新增借阅记录
     */
    @PreAuthorize("@ss.hasPermi('management:borrow:add')")
    @Log(title = "借阅记录", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody LibraryBorrow libraryBorrow)
    {
        return toAjax(libraryBorrowService.insertLibraryBorrow(libraryBorrow));
    }

    /**
     * 修改借阅记录
     */
    @PreAuthorize("@ss.hasPermi('management:borrow:edit')")
    @Log(title = "借阅记录", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody LibraryBorrow libraryBorrow)
    {
        return toAjax(libraryBorrowService.updateLibraryBorrow(libraryBorrow));
    }

    /**
     * 删除借阅记录
     */
    @PreAuthorize("@ss.hasPermi('management:borrow:remove')")
    @Log(title = "借阅记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(libraryBorrowService.deleteLibraryBorrowByIds(ids));
    }
}
